
#include <assert.h>
#include "ConfigDialog.h"
#include "tinyxml/tinyxml.h"
#include <wx/stdpaths.h>
#include <wx/filename.h>

////////////////////////////////////////////////////////////////////////////////
// ConfigSettings
////////////////////////////////////////////////////////////////////////////////

ConfigSettings* ConfigSettings::mInstance = 0;

//Constructor
ConfigSettings::ConfigSettings()
{
    mIsValid = ReadSettings();
}

//GetInstance
ConfigSettings& ConfigSettings::GetInstance()
{
    if ( !mInstance )
        mInstance = new ConfigSettings;
    assert( mInstance );
    return *mInstance;
}

//GetInstancePtr
ConfigSettings* ConfigSettings::GetInstancePtr()
{
    if ( !mInstance )
        mInstance = new ConfigSettings;
    return mInstance;
}

bool ConfigSettings::ReadSettings()
{
    bool isValid = true;

    // Open the configuration file and read the settings
    //wxStandardPaths::Get();
    wxString appDir = wxStandardPaths::Get().GetExecutablePath();
    wxFileName appPath( appDir );
    appDir = appPath.GetPath() + wxFileName::GetPathSeparator();
    TiXmlDocument doc( appDir + wxT( "config.xml" ) );
    bool success = doc.LoadFile();
    if ( success )
    {
        TiXmlNode* node = doc.FirstChild( "config" );
        if ( node )
        {
            node = node->FirstChild( "fontDirectory" );
            if ( node )
            {
                node = node->FirstChild();
                mFontDirectory = node->Value();
            }
            else
                isValid = false;
        }
        else
            isValid = false;
    }
    else
        isValid = false;

    return isValid;
}

//WriteSettings
void ConfigSettings::WriteSettings() const
{
    TiXmlDocument doc;  
    TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
    doc.LinkEndChild( decl ); 
 
    TiXmlElement* root = new TiXmlElement( "config" );  
    doc.LinkEndChild( root );

    TiXmlElement* node = new TiXmlElement( "fontDirectory" );
    root->LinkEndChild( node );

    TiXmlText* text = new TiXmlText( mFontDirectory.c_str() );
    node->LinkEndChild( text );
 
    wxString appDir = wxStandardPaths::Get().GetExecutablePath();
    wxFileName appPath( appDir );
    appDir = appPath.GetPath() + wxFileName::GetPathSeparator();
    doc.SaveFile( appDir + wxT( "config.xml" ) );  
}

//IsValid
bool ConfigSettings::IsValid() const
{
    return mIsValid;
}


////////////////////////////////////////////////////////////////////////////////
// ConfigDialog
////////////////////////////////////////////////////////////////////////////////

ConfigDialog::ConfigDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
    : ConfigDlg( parent, id, title, pos, size, style )
{
    // Initialize the values:
    ConfigSettings settings = ConfigSettings::GetInstance();
    mConfigFontDir->SetPath( settings.mFontDirectory );
}

ConfigDialog::~ConfigDialog(void)
{
}

void ConfigDialog::OnOK( wxCommandEvent& event )
{
    ConfigSettings settings = ConfigSettings::GetInstance();
    settings.mFontDirectory = mConfigFontDir->GetPath();

    settings.WriteSettings();

    ConfigDlg::OnOK( event );
}
